package com.rayboo.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 分页处理类
 * 
 * @author ray
 *
 */
public class GridPaging {
	
	private int start = 0;
	private int page = 0;
	private int length = 30;
	private int draw;
	private int totalPage = 0;
	private int totalRecord = 0;

	public Pageable genPageable() {
		page = start / length;
		return new PageRequest(page>0?(page-1):page, length);
	}

	
	public Pageable genPageable(int total) {
		page = start / length;
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		if (request.getParameter("page") != null) {
			start = length * page;
			page = Integer.parseInt(request.getParameter("page").toString());
		}
		if (total > 0) {
			totalRecord = total;
			totalPage = total % length==0?total / length:total / length+1;
		}
		return new PageRequest(page>0?(page-1):page, length);
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw + 1;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

}
